<template>
    <view class="main"
        :class="[{'main-padding': (!showTimestampTips && !loading)}]"
    >
        <view class="toolbar-left"
            @tap.stop="closeApp()"
            v-show="false"
        >
            <image class="toolbar-left-icon"
                src="/static/imgs/icon/home_back.png"
            ></image>
        </view>
        <!-- 维护信息或者非政务微信环境提示 -->
        <view class="timestampTips" v-if="showTimestampTips">
            <image
                class="timestampTips-image"
                src="/static/imgs/icon/timestampTips.png"
            ></image>
            {{ systemMaintenanceMessage }}
        </view>
        <!-- 登录中显示 -->
        <skeleton v-if="!showTimestampTips"
            :loading="loading"
            :flexType="skeleton1.flexType"
            :imgTitle="skeleton1.imgTitle"
            :row="skeleton1.row"
            :showAvatar="skeleton1.showAvatar"
            :avatarSize="skeleton1.avatarSize"
            :avatarShape="skeleton1.avatarShape"
            :nameRow="skeleton1.nameRow"
            :nameLine="skeleton1.nameLine"
            :showTitle="skeleton1.showTitle"
        >
            <!-- <view class="section-content">我是段落1</view> -->
        </skeleton>
        <!-- 登录完成，未配置项目跳转路径显示 -->
        <view v-if="!loading && !showTimestampTips"
            class="main-wrap"
        >
            <view v-if="userIsAuthorized == 0"
                class="timestampTips"
            >
                <image
                    class="authorized-image"
                    src="/static/imgs/icon/authorized.png"
                ></image>
                {{ authorizedMsg }}
            </view>
        </view>
    </view>
</template>

<script>
import Skeleton from "@/components/j-skeleton/j-skeleton.vue"
import { login } from '@/utils/login.js';
import { getStorageToken } from "@/utils/storageHelper.js";
import { wechatBrower, getUrlKey } from "@/utils/common.js";

export default {
    components: {
        Skeleton
    },
    data() {
        return {
            loading: true,
            skeleton1: {
                flexType: "center",
                imgTitle: false,
                avatarSize: "96px",
                avatarShape: "square",
                row: 3,
                nameRow: 3,
                nameLine: 3,
                showAvatar: false,
                showTitle: true,
            },
            authorizedMsg: '用户未授权',
            showTimestampTips: false,
            systemMaintenanceStart: '2022-03-17 15:02:00',
            systemMaintenanceEnd: '2022-03-17 15:12:00',
            systemMaintenanceMessage: "",
            // 判断用户是否登录
            userIsAuthorized: '-1',
        };
    },
    computed: {
		/**
		 * userid
		 */
		userId() {
			if (!this.$route.query.userId) {
				return "";
			}
			return this.$route.query.userId;
		},
    },
    onLoad() {
    },
    onShow() {
    },
    mounted() {
    },
    async created() {
        let __this = this;
        if (process.env.VUE_APP_PACKAGE && process.env.VUE_APP_PACKAGE == "rayTemplate") {
            __this.skeleton1 = {
                flexType: "center",
                imgTitle: true,
                avatarSize: "96px",
                avatarShape: "square",
                row: 3,
                nameRow: 3,
                nameLine: 3,
                showAvatar: true,
                showTitle: false,
            };
        }

        /**
         * 判断是否政务微信浏览器打开，否则不执行任务操作
         * add by ray on 2022/04/23
         * 暂时不做拦截 modify by ray on 2022/05/06
         * */
        // let fromWechat = wechatBrower();
        // if (!fromWechat) {
        //     __this.showTimestampTips = true;
        //     __this.systemMaintenanceMessage = "请在粤政易中打开";
        //     return;
        // }

        // 系统维护中判断
        // __this.systemMaintenance();

        console.log(process.env);
        console.log("getStorageToken(loginToken) => ", getStorageToken("loginToken"));
        if (
            (process.env.VUE_APP_LOGIN_MODE && "login" == process.env.VUE_APP_LOGIN_MODE)
            && !getStorageToken("loginToken")
        ) {
            uni.reLaunch({
                url: `/pages/login/login`,
            });
            return;
        }

        if (!getStorageToken("loginToken")) {
            uni.showLoading({ title: "加载中", mask: true });
            await login();
            uni.hideLoading();
        }

        __this.loading = false;

        if (!getStorageToken("loginToken")) {
            __this.showTimestampTips = true;
            __this.systemMaintenanceMessage = "还未登录，请先登录";
            return;
        }

        if (process.env.VUE_APP_PACKAGE) {
            let path = "";
            let paramCode = "code";
            let paramState = "state";
            let valueCode = getUrlKey(paramCode, window.location.href);
            let valueState = getUrlKey(paramState, window.location.href);
            let suffix = "?";
            switch (process.env.VUE_APP_PACKAGE) {
                case "rayTemplate":
                    // 块事通
                    path = "/pagesRayTemplate/pages/index/index";
                    break;
            }
            if (path) {
                uni.redirectTo({
                    // url: `${path}${suffix}${paramCode}=${valueCode}&${paramState}=${valueState}`,
                    url: `${path}`,
                });
                return;
            }
        }

        __this.userIsAuthorized = 0;
        __this.authorizedMsg = "未配置项目路径";

    },
    watch: {
    },
    methods: {
        closeApp () {},
        /**
         * @description: 系统维护中判断
         * @param {*}
         * @return {*}
         */
        systemMaintenance() {
            var curTimestamp = new Date().getTime()
            // 开始时间
            let startTimestamp = toTimeStamp(this.systemMaintenanceStart)
            // 结束时间
            let endTimestamp = toTimeStamp(this.systemMaintenanceEnd)
            this.systemMaintenanceMessage = "为提供更好的服务，将定于2022年03月14日（周一）22:00-2022年03月15日（周二）06:00进行系统升级维护，对您造成的不便，敬请谅解";

            if (curTimestamp > startTimestamp && curTimestamp < endTimestamp) {
                this.showTimestampTips = true
                return
            }
        },
    },
};
</script>

<style lang="scss" scoped >
uni-page-body {
    width: 100vw;
    overflow: hidden;
    background: #fff;
}

.main-padding {
    padding-bottom: 100rpx;
}
.main {
    height: 100%;
    width: 100%;

    .toolbar-left {
        position: fixed;
        top: 15rpx;
        left: 15rpx;
        z-index: 998;
        font-size: 30rpx;
        color: #000;

        .toolbar-left-icon {
            width: 60rpx;
            height: 60rpx;
        }
    }
    .timestampTips {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        // justify-content: center;
        color: #999;
        flex-direction: column;
        padding: 0 60rpx;
        padding-top: 300rpx;
        font-size: 34rpx;
        .timestampTips-image {
            width: 100rpx;
            height: 80rpx;
            margin-bottom: 60rpx;
        }
        .authorized-image {
            width: 250rpx;
            height: 250rpx;
            margin-top: 160rpx;
            margin-bottom: 100rpx;
        }

        .loading-image {
            width: 100rpx;
            height: 100rpx;
            margin-top: 100rpx;
            margin-bottom: 50rpx;
        }
    }
}
</style>